var stats_goodsStatus ;
var parcelorderCtrl  = new XFApp.client();
$.views.converters({
    subString : function(val){
        return parcelorderCtrl.subString(val,7);
    },

    formatDate : function(val){
        var date = new Date(val);
        return date.format("yyyy-MM-dd hh:mm:ss");
    },

    getCurrencys : function(){
        return dictCtrl.dictionary.translate("currency");

    },

    formatCurrency : function(val){

        if(!val){
            return "";
        }

        var result = "未知类型("+val+")" ;
        for(var index in currencys){
            var dict = currencys[index];
            if(dict.dictCode == val){
                result = dict.dictName;
                break;
            }
        }
        return result;
    },

    ftime : function(val){
        if(!!val){
            return val.year+"-"+val.monthValue+"-"+val.dayOfMonth+" "+val.hour+":"+val.minute+":"+val.second;
        }
        return "";
    },

    fgoodsStatus : function(value) {
        if(!value){
            return "";
        }
        for(index in stats_goodsStatus){
            var goodsStatus = stats_goodsStatus[index];
            if(goodsStatus.dictCode == value){
                return goodsStatus.dictName;
            }
        }
        return  "未知类型("+value+")";
    },

});
XFApp.client.prototype.parcelorder = {
    parcelOrderPage : null,
    smallPage:null,
    successCode : 10000,
    detailId:null,
    selPage:null,
    goodsResult:null,
    weight:null,
    urlItems : {
        importDataURL:basePath+"/nt/newIm/importData",
        downLoadURL:basePath+"/nt/parcelOrder/downloadTemplate",
    },

    skuSelector :function(idx){
        _this = this;
        _this.detailId = idx;
        _this.selSkuPage();
        $("#choose_sku_modal").modal();
    },

    formValidate : function(){
        $("#importForm").validate({
            rules: {
                excelFile : "required"
            },
            messages: {
                excelFile: "请上传数据文件"
            }
        });
        $("#importForm").validate();
    },

    formValidate2 : function(){
        $("#importForm2").validate({
            rules: {
                excelBigPkgFile : "required"
            },
            messages: {
                excelBigPkgFile: "请上传数据文件"
            }
        });
        $("#importForm2").validate();
    },

    showSKU:function(sku,detailId){

        if(!sku){
            window.webDialogUtil.alert("提示","请先选择SKU，再进行查看");
            return;
        }

        _this.detailId = detailId;

        var orderDetail = parcelorderCtrl.parcelorder.findDetail(detailId);

        var goods = parcelorderCtrl.parcelorder.findGoods(detailId);

        parcelorderCtrl.parcelorder.openData(orderDetail,goods,detailId);

    },


    enableAll: function (){

        // $("#submit").attr("href","#");
        //设置disabled属性
        $("#submit").attr("disabled", false);

        $("button[name='delOrOpen']").attr({"disabled":false});
        $("button[name='bak']").attr({"disabled":false});
        $("button[name='sku_choose']").attr({"disabled":false});
        $("button[name='sku_remove']").attr({"disabled":false});
        $("button[name='copy']").attr({"disabled":false});
        $("input[name='sku']").attr({"readonly":false});
        $("input[name='qlt']").attr({"readonly":false});
        $("input[name='price']").attr({"readonly":false});
        $("input[name='goodsWeight']").attr({"readonly":false});
    },


    disableAll:function(){
        // $("#submit").attr("href","#");
        //设置disabled属性
        $("#submit").attr("disabled", true);

        $("button[name='delOrOpen']").attr({"disabled":true});
        $("button[name='bak']").attr({"disabled":true});
        $("button[name='sku_choose']").attr({"disabled":true});
        $("button[name='sku_remove']").attr({"disabled":true});
        $("button[name='copy']").attr({"disabled":true});
        $("input[name='sku']").attr({"readonly":true});
        $("input[name='qlt']").attr({"readonly":true});
        $("input[name='price']").attr({"readonly":true});
        $("input[name='goodsWeight']").attr({"readonly":true});
    },

    //复制
    copyToClipboard: function (){
        $('.table-link').each(function(i){
            var id = $(this).attr('data');
            var path = remoteRes+"/js/plugins/zeroclipboard/ZeroClipboard.swf" ;
            ZeroClipboard.config({swfPath:path});
            var client = new ZeroClipboard($(this));
            client.on('copy', function(event) {
                event.clipboardData.setData('text/plain', $("#copyTxt"+id).val());
            });
            client.on('aftercopy', function(event) {
                window.webDialogUtil.alert("提示",'复制成功');
            });
            client.on('error', function(event) {
                console.log( 'ZeroClipboard error of type "' + event.name + '": ' + event.message );
                ZeroClipboard.destroy();
            });
        });
    },
    formValidate : function(){
        $("#importForm").validate({
            rules: {
                excelFile : "required"
            },
            messages: {
                excelFile: "请上传数据文件"
            }
        });
        $("#importForm").validate();
    },
    importData:function (){
        var _this = this;
        var params = [];
        $("input").each(function(){
            if(!!$(this).val() && !!$(this).attr("id")){
                var param = {name:'',value:''};
                param.name=$(this).attr("id");
                param.value=$(this).val();
                params.push(param);
            }
        });
        webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
            //基于ajaxfileUpload.js实现
            $.ajaxFileUpload({
                url : _this.urlItems.importDataURL,
                secureuri : false,
                fileElementId : 'excelFile',//file标签的id
                dataType : 'json',
                data : params,
                success: function (data, status) {
                    webDialogUtil.simpleAlert(data.msg, "温馨提示", null, 300, 10);
                },
                error: function (data, status, e) {
                    webDialogUtil.simpleAlert("处理异常", "温馨提示");
                }
            });
        });

    },

    downloadTemplate: function(fileName){
        _this = this;
        var url = _this.urlItems.downLoadURL+"?fileName="+fileName;
        var iframeId = "yd_template_download_frame" ;
        $("body").append("<iframe id='"+iframeId+"' src='" + url +"' frameborder=0 width=0 height=0 style='display:none'></iframe>");
    },

    event : function(){
        var _this = this;

        $("#importButton").on("click",function(){
            if($("#importForm").validate().form()){
                parcelorderCtrl.parcelorder.importData();
            }
        });

        $("#downloadButton").on("click", function(){
            parcelorderCtrl.parcelorder.downloadTemplate('yd_template.xls');
        });
        //表单验证
        _this.formValidate();
    },
    initStatus:function(){
        stats_goodsStatus = dictCtrl.dictionary.translate("goodsDocketStatus");
        $.views.converters({
            fgoodsStatus : function(value) {
                if(!value){
                    return "";
                }
                for(index in stats_goodsStatus){
                    var goodsStatus = stats_goodsStatus[index];
                    if(goodsStatus.dictCode == value){
                        return goodsStatus.dictName;
                    }
                }
                return  "未知类型("+value+")";
            }
        });
    }
};

$(function(){
    parcelorderCtrl.parcelorder.event();
    parcelorderCtrl.parcelorder.initStatus();
});
